<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle">
    <el-form
      :model="formData"
      ref="formRef"
      :rules="formRules"
      vloading="formLoading"
      :inline="true"
      label-width="auto"
    >
      <el-form-item label="服务器名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入服务器名称" :clearable="true" />
      </el-form-item>
      <el-form-item label="服务器ip" prop="ip">
        <el-input
          v-model="formData.ip"
          placeholder="请输入服务器ip"
          :clearable="true"
          @keyup="formData.ip = formData.ip.replace(/[^0-9.]/g, '')"
        />
      </el-form-item>
      <el-form-item label="服务器密码" prop="serverPassword">
        <el-input
          v-model="formData.serverPassword"
          placeholder="请输入服务器密码"
          :clearable="true"
        />
      </el-form-item>
      <el-form-item label="域名" prop="domainName">
        <el-input v-model="formData.domainName" placeholder="请输入域名" :clearable="true" />
      </el-form-item>
      <el-form-item label="内网IP" prop="internalIp">
        <el-input v-model="formData.internalIp" placeholder="请输入内网IP" :clearable="true" />
      </el-form-item>
      <el-form-item label="数据库密码" prop="databasePassword">
        <el-input
          v-model="formData.databasePassword"
          placeholder="请输入数据库密码"
          :clearable="true"
        />
      </el-form-item>
      <el-form-item label="后端服务端口" prop="port">
        <el-input v-model="formData.port" placeholder="请输入服务端口" :clearable="true" />
      </el-form-item>
      <el-form-item label="前端服务端口" prop="frontPort">
        <el-input v-model="formData.frontPort" placeholder="请输入服务端口" :clearable="true" />
      </el-form-item>
      <el-form-item label="唯一码" prop="uniqueCode">
        <el-input
          v-model="formData.uniqueCode"
          placeholder="请输入唯一码"
          :clearable="true"
          style="width: 300px"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" :disabled="formLoading" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup name="ServerEdit" lang="ts">
import * as DevopsServerApi from '@/api/infra/devops/server'
let formType = ''
const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const dialogTitle = ref('')
const dialogVisible = ref(false)
const formRef = ref()
const formLoading = ref(false)
const formData = ref({
  id: undefined,
  name: '',
  ip: '',
  internalIp: '',
  serverPassword: '',
  databasePassword: '',
  port: 0,
  frontPort: 0,
  uniqueCode: '',
  domainName: ''
})
const formRules = reactive({
  ip: [{ required: true, message: '服务器ip不能为空', trigger: 'blur' }],
  serverPassword: [{ required: true, message: '服务器密码不能为空', trigger: 'blur' }],
  databasePassword: [{ required: true, message: '数据库密码不能为空', trigger: 'blur' }],
  port: [{ required: true, message: '服务端口不能为空', trigger: 'blur' }]
})
/** 打开弹窗 */
const open = async (type, id) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  resetForm()
  formType = type
  if (type == 'update') {
    formData.value = await DevopsServerApi.singleGet(id)
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    name: '',
    ip: '',
    internalIp: '',
    serverPassword: '',
    databasePassword: '',
    port: 0,
    frontPort: 0,
    uniqueCode: '',
    domainName: ''
  }
  formRef.value?.resetFields()
}

const emit = defineEmits(['success'])
/** 提交表单 */
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return

  // 提交请求
  formLoading.value = true
  try {
    if (formType === 'create') {
      await DevopsServerApi.create(formData.value)
      message.success(t('common.createSuccess'))
    } else {
      await DevopsServerApi.update(formData.value)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}
</script>
